home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / win / winmain.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  9KB  |  410 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: winmain.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5. /* GNUPLOT - win/winmain.c */
  6. /*
  7.  * Copyright (C) 1992   Maurice Castro, Russell Lang
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  *
  22.  * AUTHORS
  23.  * 
  24.  *   Maurice Castro
  25.  *   Russell Lang
  26.  * 
  27.  * Send your comments or suggestions to 
  28.  *  info-gnuplot@dartmouth.edu.
  29.  * This is a mailing list; to join it send a note to 
  30.  *  info-gnuplot-request@dartmouth.edu.  
  31.  * Send bug reports to
  32.  *  bug-gnuplot@dartmouth.edu.
  33.  */
  34.  
  35. /* This file implements the initialization code for running gnuplot   */
  36. /* under Microsoft Windows. The code currently compiles only with the */
  37. /* Borland C++ 3.1 compiler. 
  38. /*                                                                    */
  39. /* The modifications to allow Gnuplot to run under Windows were made  */
  40. /* by Maurice Castro. (maurice@bruce.cs.monash.edu.au)  3 Jul 1992    */
  41. /* and Russell Lang (rjl@monu1.cc.monash.edu.au) 30 Nov 1992          */
  42. /*                                                                    */
  43.  
  44. #define STRICT
  45. #include <windows.h>
  46. #include <windowsx.h>
  47. #include <dos.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <stdarg.h>
  52. #ifdef __MSC__
  53. #include <malloc.h>
  54. #else
  55. #include <alloc.h>
  56. #endif
  57. #include <io.h>
  58. #include "plot.h"
  59. #include "setshow.h"
  60. #include "wgnuplib.h"
  61. #include "wtext.h"
  62.  
  63. /* limits */
  64. #define MAXSTR 255
  65. #define MAXPRINTF 1024
  66.  
  67. /* globals */
  68. TW textwin;
  69. GW graphwin;
  70. PW pausewin;
  71. MW menuwin;
  72. LPSTR szModuleName;
  73. LPSTR winhelpname;
  74. LPSTR szMenuName;
  75. #define MENUNAME "wgnuplot.mnu"
  76.  
  77. extern char version[];
  78. extern char patchlevel[];
  79. extern char date[];
  80. extern char *authors[];
  81. extern char copyright[];
  82. extern void close_printer();
  83. void WinExit(void);
  84. int gnu_main(int argc, char *argv[], char *env[]);
  85.  
  86. void
  87. CheckMemory(LPSTR str)
  88. {
  89.     if (str == (LPSTR)NULL) {
  90.         MessageBox(NULL, "out of memory", "gnuplot", MB_ICONSTOP | MB_OK);
  91.         exit(1);
  92.     }
  93. }
  94.  
  95. int
  96. Pause(LPSTR str)
  97. {
  98.     pausewin.Message = str;
  99.     return (PauseBox(&pausewin) == IDOK);
  100. }
  101.  
  102. /* atexit procedure */
  103. void
  104. WinExit(void)
  105. {
  106.     if (term && term_init)
  107.         (*term_tbl[term].reset)();
  108.     if ( !strcmp(outstr,"'PRN'") )
  109.         close_printer();
  110.     fcloseall();
  111.     if (graphwin.hWndGraph && IsWindow(graphwin.hWndGraph))
  112.         GraphClose(&graphwin);
  113.     TextMessage();    /* process messages */
  114.      WinHelp(textwin.hWndText,(LPSTR)winhelpname,HELP_QUIT,(DWORD)NULL);
  115.     TextClose(&textwin);
  116.     TextMessage();    /* process messages */
  117.     return;
  118. }
  119.  
  120. /* call back function from Text Window WM_CLOSE */
  121. int CALLBACK _export
  122. ShutDown(void)
  123. {
  124.     WinExit();
  125.     exit(0);
  126.     return 0;
  127. }
  128.  
  129. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  130.         LPSTR lpszCmdLine, int nCmdShow)
  131. {
  132.     WNDCLASS wndclass;
  133.     LPSTR tail;
  134.     
  135. #if defined(__MSC__)    /* MSC doesn't give us _argc and _argv[] so ...   */
  136. #define MAXCMDTOKENS 128
  137.     int     _argc=0;
  138.     LPSTR   _argv[MAXCMDTOKENS];
  139.     _argv[_argc] = "wgnuplot.exe";
  140.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  141.     while (_argv[_argc] != NULL)
  142.         _argv[++_argc] = _fstrtok( NULL, " ");
  143. #endif
  144.  
  145.       szModuleName = (LPSTR)farmalloc(MAXSTR+1);
  146.       CheckMemory(szModuleName);
  147.     szModuleName = (LPSTR)farmalloc(MAXSTR+1);
  148.     CheckMemory(szModuleName);
  149.  
  150.     /* get path to EXE */
  151.     GetModuleFileName(hInstance, (LPSTR) szModuleName, MAXSTR);
  152.     if (CheckWGNUPLOTVersion(WGNUPLOTVERSION)) {
  153.         MessageBox(NULL, "Wrong version of WGNUPLOT.DLL", szModuleName, MB_ICONSTOP | MB_OK);
  154.         exit(1);
  155.     }
  156.     if ((tail = _fstrrchr(szModuleName,'\\')) != (LPSTR)NULL)
  157.     {
  158.         tail++;
  159.         *tail = NULL;
  160.     }
  161.     szModuleName = farrealloc(szModuleName, _fstrlen(szModuleName)+1);
  162.     CheckMemory(szModuleName);
  163.  
  164.     winhelpname = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(HELPFILE)+1);
  165.     CheckMemory(winhelpname);
  166.     _fstrcpy(winhelpname,szModuleName);
  167.     _fstrcat(winhelpname,HELPFILE);
  168.  
  169.     szMenuName = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(MENUNAME)+1);
  170.     CheckMemory(szMenuName);
  171.     _fstrcpy(szMenuName,szModuleName);
  172.     _fstrcat(szMenuName,MENUNAME);
  173.  
  174.     textwin.hInstance = hInstance;
  175.     textwin.hPrevInstance = hPrevInstance;
  176.     textwin.nCmdShow = nCmdShow;
  177.     textwin.Title = "gnuplot";
  178.     textwin.IniFile = "wgnuplot.ini";
  179.     textwin.IniSection = "WGNUPLOT";
  180.     textwin.DragPre = "load '";
  181.     textwin.DragPost = "'\n";
  182.     textwin.lpmw = &menuwin;
  183.     textwin.ScreenSize.x = 80;
  184.     textwin.ScreenSize.y = 80;
  185.     textwin.KeyBufSize = 2048;
  186.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  187.     textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance);
  188.     textwin.AboutText = (LPSTR)farmalloc(1024);
  189.     CheckMemory(textwin.AboutText);
  190.     sprintf(textwin.AboutText,"Version %s\nPatchlevel %s\nLast Modified %s\n%s\n%s, %s",
  191.         version, patchlevel, date, copyright, authors[1], authors[0]);
  192.     textwin.AboutText = farrealloc(textwin.AboutText, _fstrlen(textwin.AboutText)+1);
  193.     CheckMemory(textwin.AboutText);
  194.  
  195.     menuwin.szMenuName = szMenuName;
  196.  
  197.     pausewin.hInstance = hInstance;
  198.     pausewin.hPrevInstance = hPrevInstance;
  199.     pausewin.Title = "gnuplot pause";
  200.  
  201.     graphwin.hInstance = hInstance;
  202.     graphwin.hPrevInstance = hPrevInstance;
  203.     graphwin.Title = "gnuplot graph";
  204.     graphwin.lptw = &textwin;
  205.     graphwin.IniFile = textwin.IniFile;
  206.     graphwin.IniSection = textwin.IniSection;
  207.     graphwin.color=TRUE;
  208.     graphwin.fontsize = WINFONTSIZE;
  209.  
  210.     if (TextInit(&textwin))
  211.         exit(1);
  212.     textwin.hIcon = LoadIcon(hInstance, "texticon");
  213. #ifdef WIN32
  214.     SetClassLong(textwin.hWndParent, GCL_HICON, (DWORD)textwin.hIcon);
  215. #else
  216.     SetClassWord(textwin.hWndParent, GCW_HICON, (WORD)textwin.hIcon);
  217. #endif
  218.     if (_argc>1)
  219.         ShowWindow(textwin.hWndParent,SW_SHOWMINIMIZED);
  220.     if (IsIconic(textwin.hWndParent)) { /* update icon */
  221.         RECT rect;
  222.         GetClientRect(textwin.hWndParent, (LPRECT) &rect);
  223.         InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1);
  224.         UpdateWindow(textwin.hWndParent);
  225.     }
  226.  
  227.  
  228.     atexit(WinExit);
  229.  
  230.     gnu_main(_argc, _argv, environ);
  231.  
  232.     return 0;
  233. }
  234.  
  235.  
  236. /* replacement stdio routines that use Text Window for stdin/stdout */
  237. /* WARNING: Do not write to stdout/stderr with functions not listed 
  238.    in win/wtext.h */
  239.  
  240. #undef kbhit
  241. #undef getche
  242. #undef getch
  243. #undef putch
  244.  
  245. #undef fgetc
  246. #undef getchar
  247. #undef getc
  248. #undef fgets
  249. #undef gets
  250.  
  251. #undef fputc
  252. #undef putchar
  253. #undef putc
  254. #undef fputs
  255. #undef puts
  256.  
  257. #undef fprintf
  258. #undef printf
  259. #undef vprintf
  260. #undef vfprintf
  261.  
  262. #undef fwrite
  263. #undef fread
  264.  
  265. #ifdef __MSC__
  266. #define isterm(f) (f==stdin || f==stdout || f==stderr)
  267. #else
  268. #define isterm(f) isatty(fileno(f))
  269. #endif
  270.  
  271. int
  272. MyPutCh(int ch)
  273. {
  274.     return TextPutCh(&textwin, (BYTE)ch);
  275. }
  276.  
  277. int
  278. MyKBHit(void)
  279. {
  280.     return TextKBHit(&textwin);
  281. }
  282.  
  283. int
  284. MyGetCh(void)
  285. {
  286.     return TextGetCh(&textwin);
  287. }
  288.  
  289. int
  290. MyGetChE(void)
  291. {
  292.     return TextGetChE(&textwin);
  293. }
  294.  
  295. int
  296. MyFGetC(FILE *file)
  297. {
  298.     if (isterm(file)) {
  299.         return MyGetChE();
  300.     }
  301.     return fgetc(file);
  302. }
  303.  
  304. char *
  305. MyGetS(char *str)
  306. {
  307.     TextPutS(&textwin,"\nDANGER: gets() used\n");
  308.     MyFGetS(str,80,stdin);
  309.     if (strlen(str) > 0 
  310.      && str[strlen(str)-1]=='\n')
  311.         str[strlen(str)-1] = '\0';
  312.     return str;
  313. }
  314.  
  315. char *
  316. MyFGetS(char *str, unsigned int size, FILE *file)
  317. {
  318. char FAR *p;
  319.     if (isterm(file)) {
  320.         p = TextGetS(&textwin, str, size);
  321.         if (p != (char FAR *)NULL)
  322.             return str;
  323.         return (char *)NULL;
  324.     }    
  325.     return fgets(str,size,file);
  326. }
  327.  
  328. int
  329. MyFPutC(int ch, FILE *file)
  330. {
  331.     if (isterm(file)) {
  332.         MyPutCh((BYTE)ch);
  333.         TextMessage();
  334.         return ch;
  335.     }
  336.     return fputc(ch,file);
  337. }
  338.  
  339. int
  340. MyFPutS(char *str, FILE *file)
  341. {
  342.     if (isterm(file)) {
  343.         TextPutS(&textwin, str);
  344.         TextMessage();
  345.         return (*str);    /* different from Borland library */
  346.     }
  347.     return fputs(str,file);
  348. }
  349.  
  350. int
  351. MyPutS(char *str)
  352. {
  353.     TextPutS(&textwin, str);
  354.     MyPutCh('\n');
  355.     TextMessage();
  356.     return 0;    /* different from Borland library */
  357. }
  358.  
  359. int MyFPrintF(FILE *file, char *fmt, ...)
  360. {
  361. int count;
  362. va_list args;
  363.     va_start(args,fmt);
  364.     if (isterm(file)) {
  365.         char buf[MAXPRINTF];
  366.         count = vsprintf(buf,fmt,args);
  367.         TextPutS(&textwin,buf);
  368.     }
  369.     else
  370.         count = vfprintf(file, fmt, args);
  371.     va_end(args);
  372.     return count;
  373. }
  374.  
  375. int MyPrintF(char *fmt, ...)
  376. {
  377. int count;
  378. char buf[MAXPRINTF];
  379. va_list args;
  380.     va_start(args,fmt);
  381.     count = vsprintf(buf,fmt,args);
  382.     TextPutS(&textwin,buf);
  383.     va_end(args);
  384.     return count;
  385. }
  386.  
  387. size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *file)
  388. {
  389.     if (isterm(file)) {
  390.         int i;
  391.         for (i=0; i<n; i++)
  392.             TextPutCh(&textwin, ((BYTE *)ptr)[i]);
  393.         TextMessage();
  394.         return n;
  395.     }
  396.     return fwrite(ptr, size, n, file);
  397. }
  398.  
  399. size_t MyFRead(void *ptr, size_t size, size_t n, FILE *file)
  400. {
  401.     if (isterm(file)) {
  402.         int i;
  403.         for (i=0; i<n; i++)
  404.             ((BYTE *)ptr)[i] = TextGetChE(&textwin);
  405.         TextMessage();
  406.         return n;
  407.     }
  408.     return fread(ptr, size, n, file);
  409. }
  410.